package com.easondongh.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import com.easondongh.system.constants.Constants;
import com.easondongh.system.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.system.mapper.SysRoleMapper;
import com.easondongh.system.domain.SysRole;
import com.easondongh.system.service.SysRoleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author EasonDongH
 * @date 2020/5/11 9:24
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public PageResult<SysRole> getByPage(BasePageQueryVo<RoleQueryVo> queryCondition) {
        IPage<SysRole> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        RoleQueryVo queryParams = queryCondition.getQueryParams();
        if(queryParams != null) {
        }
        IPage<SysRole> iPage = this.roleMapper.selectPage(page, queryWrapper);
        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    @Transactional
    public void dispatchMenuAndPermission(RoleDispatchVo dispatchVo) {
        this.roleMapper.deleteMenuByRid(dispatchVo.getRid());
        this.roleMapper.deletePermissionByRid(dispatchVo.getRid());

        List<Integer> menus = dispatchVo.getMenus();
        if(menus != null && menus.size() > 0) {
            this.roleMapper.insertMenus(dispatchVo.getRid(), menus);
        }
        List<Integer> permissions = dispatchVo.getPermissions();
        if(permissions != null && permissions.size() > 0) {
            this.roleMapper.insertPermissions(dispatchVo.getRid(), permissions);
        }
    }

    @Override
    public RoleDispatchVo getMenuAndPermission(Integer rid) {
        RoleDispatchVo dispatchVo = new RoleDispatchVo();
        dispatchVo.setRid(rid);
        dispatchVo.setMenus(this.roleMapper.getMenusByRid(rid));
        dispatchVo.setPermissions(this.roleMapper.getPermissionByRid(rid));
        return dispatchVo;
    }

    @Override
    @Transactional
    public boolean removeById(Integer id) {
        this.roleMapper.deleteMenuByRid(id);
        this.roleMapper.deletePermissionByRid(id);
        this.roleMapper.deleteUserByRid(id);
        super.removeById(id);
        return true;
    }

    @Override
    public List<SysRole> loadEnableRole() {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysRole.COL_ENABLE, Constants.ENABLE);
        List<SysRole> roles = this.roleMapper.selectList(queryWrapper);
        return roles;
    }

    @Override
    public List<String> getRoleNamesByUid(Integer id) {
        List<Integer> rids = this.roleMapper.getRidsByUid(id);
        if(!CollectionUtils.isEmpty(rids)) {
            QueryWrapper<SysRole> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.eq(SysRole.COL_ENABLE, Constants.ENABLE);
            roleQueryWrapper.in(SysRole.COL_ID, rids);
            List<SysRole> roles = this.roleMapper.selectList(roleQueryWrapper);
            if(!CollectionUtils.isEmpty(roles)) {
                List<String> list = roles.stream().map(r -> r.getName()).collect(Collectors.toList());
                return list;
            }
        }
        return null;
    }
}
